package com.qumitech.visualnovel.service.block.impl;

import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.component.exception.BusinessException;
import com.qumitech.visualnovel.component.util.HttpInvokeUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.controller.vo.block.BlockApplicationConfigVO;
import com.qumitech.visualnovel.controller.vo.block.BlockPlanConfigVO;
import com.qumitech.visualnovel.model.common.UrlConstant;
import com.qumitech.visualnovel.pubdef.dto.blockPosition.BlockPositionDTO;
import com.qumitech.visualnovel.service.block.BlockService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author :  ZhChNing
 * @Descriptiom :
 * @Date Created in 2019/3/11 9:47
 */
@Service
public class BlockServiceImpl implements BlockService {


    private final String BLOCLK_PLAN_URL = "/block/plan";
    private final String BLOCLK_PLAN_ADMIN_URL = "/admin/block/plan";
    private final String BLOCLK_appliction_URL = "/block/appliction";
    private final String BLOCLK_APPLICATION_ADMIN_URL = "/admin/block/appliction";

    private final String BLOCLK_APP_INFO_URL = "/admin/block/appinfo";
    private final String BLOCK_POSITION_CONFIG = "/block/position/config";
    private final String BLOCK_POSITION_ADMIN_CONFIG = "/admin/block/position/config";

    @Override
    public Map<String, Object> listBlockPlanConfigVO(Integer offset, Integer limit, Byte blockType, String planName, Integer planId) {
        String url = UrlConstant.SERVER_URL + BLOCLK_PLAN_ADMIN_URL;
        Map<String, String> paramMap = new HashMap<>();

        if (offset != null) {
            paramMap.put("offset", offset.toString());
        }
        if (limit != null) {
            paramMap.put("limit", limit.toString());
        }
        if (blockType != null) {
            paramMap.put("block_type", blockType.toString());
        }
        if (planName != null) {
            paramMap.put("plan_name", planName);
        }
        if (planId != null) {
            paramMap.put("plan_id", planId.toString());
        }

        String body = HttpInvokeUtil.httpGet(url, paramMap, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()
                || rsp.getData() == null) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        Map<String, Object> rspMap = (Map<String, Object>) rsp.getData();
        return rspMap;

    }

    @Override
    public Map<String, Object> listBlockPlanConfigVO(Integer planId) {
        String url = UrlConstant.SERVER_URL + BLOCLK_PLAN_ADMIN_URL + "/" + planId;
        Map<String, String> paramMap = new HashMap<>();
        String body = HttpInvokeUtil.httpGet(url, paramMap, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()
                || rsp.getData() == null) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        Map<String, Object> rspMap = (Map<String, Object>) rsp.getData();
        return rspMap;

    }

    @Override
    public Map<String, Object> addBlockPlanConfigVO(BlockPlanConfigVO blockPlanConfigVO) {
        String url = UrlConstant.SERVER_URL + BLOCLK_PLAN_URL;
        String req = JSONObject.toJSONString(blockPlanConfigVO);
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);

        }
        Map<String, Object> rspMap = (Map<String, Object>) rsp.getData();
        return rspMap;
    }

    @Override
    public void updateBlockPlanConfigVO(BlockPlanConfigVO blockPlanConfigVO) {
        String url = UrlConstant.SERVER_URL + BLOCLK_PLAN_URL + "/" + blockPlanConfigVO.getPlanId();
        String req = JSONObject.toJSONString(blockPlanConfigVO);
        String body = HttpInvokeUtil.httpPutJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
    }

    @Override
    public void removeBlockPlanConfigVO(Integer planId) {
        String url = UrlConstant.SERVER_URL + BLOCLK_PLAN_URL + "/" + planId;
        Map<String, String> headMap = new HashMap<>();
        String body = HttpInvokeUtil.httpDelete(url, headMap);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {

            Integer status = Integer.valueOf(((Map<Integer, Object>) rsp.getError()).get("status").toString());
            if (status.equals(ResultEnum.BLOCK_APP_PLAN_BE_USED.getState())) {
                throw new BusinessException(ResultEnum.BLOCK_APP_PLAN_BE_USED);
            }
            throw new BusinessException(ResultEnum.FAILURE);


        }
    }

    @Override
    public Map<String, Object> listBlockApplicationConfigVO(Integer offset, Integer limit, String channel, String version, Integer deviceType, String packageName, String applicationName) {

        String url = UrlConstant.SERVER_URL + BLOCLK_APPLICATION_ADMIN_URL;
        Map<String, String> paramMap = new HashMap<>();

        if (offset != null) {
            paramMap.put("offset", offset.toString());
        }
        if (limit != null) {
            paramMap.put("limit", limit.toString());
        }
        if (channel != null) {
            paramMap.put("channel", channel);
        }
        if (version != null) {
            paramMap.put("version", version);
        }
        if (deviceType != null) {
            paramMap.put("device_type", deviceType.toString());
        }
        if (packageName != null) {
            paramMap.put("package_name", packageName);
        }
        if (packageName != null) {
            paramMap.put("application_name", applicationName);
        }


        String body = HttpInvokeUtil.httpGet(url, paramMap, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()
                || rsp.getData() == null) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        Map<String, Object> rspMap = (Map<String, Object>) rsp.getData();
        return rspMap;
    }

    @Override
    public void addBlockApplicationConfigVO(BlockApplicationConfigVO blockApplicationConfigVO) {
        String url = UrlConstant.SERVER_URL + BLOCLK_appliction_URL;
        String req = JSONObject.toJSONString(blockApplicationConfigVO);
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {

            Integer status = Integer.valueOf(((Map<Integer, Object>) rsp.getError()).get("status").toString());

            if (status.equals(ResultEnum.BLOCK_APPLICATION_EXIST.getState())) {
                throw new BusinessException(ResultEnum.BLOCK_APPLICATION_EXIST);
            }
            throw new BusinessException(ResultEnum.FAILURE);
        }

    }

    @Override
    public void updateBlockApplicationConfigVO(BlockApplicationConfigVO blockApplicationConfigVO) {
        String url = UrlConstant.SERVER_URL + BLOCLK_appliction_URL + "/" + blockApplicationConfigVO.getApplicationId();
        String req = JSONObject.toJSONString(blockApplicationConfigVO);
        String body = HttpInvokeUtil.httpPutJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
    }

    @Override
    public void removeBlockApplicationConfigVO(Integer applicationId) {
        String url = UrlConstant.SERVER_URL + BLOCLK_appliction_URL + "/" + applicationId;
        Map<String, String> headMap = new HashMap<>();
        String body = HttpInvokeUtil.httpDelete(url, headMap);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
    }

    @Override
    public Map<String, Object> getAppInfo() {
        String url = UrlConstant.SERVER_URL + BLOCLK_APP_INFO_URL;
        Map<String, String> paramMap = new HashMap<>();
        String body = HttpInvokeUtil.httpGet(url, paramMap, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()
                || rsp.getData() == null) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        Map<String, Object> rspMap = (Map<String, Object>) rsp.getData();
        return rspMap;
    }

    @Override
    public void addBlockPosition(BlockPositionDTO blockPositionDTO) {
        String url = UrlConstant.SERVER_URL + BLOCK_POSITION_CONFIG;
        String req = JSONObject.toJSONString(blockPositionDTO);
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
    }

    @Override
    public void updateBlockPosition(BlockPositionDTO positionConfig) {
        String url = UrlConstant.SERVER_URL + BLOCK_POSITION_CONFIG;
        String body = HttpInvokeUtil.httpPutJson(url, JSONObject.toJSONString(positionConfig), UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
    }

    @Override
    public Map<String, Object> getPositionInfo(Byte viewType) {
        String url = UrlConstant.SERVER_URL + BLOCK_POSITION_ADMIN_CONFIG;
        Map<String, String> paramMap = new HashMap<>();
        if(viewType != null){
            paramMap.put("view_type", viewType.toString());
        }

        String body = HttpInvokeUtil.httpGet(url, paramMap, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()
                || rsp.getData() == null) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        Map<String, Object> rspMap = (Map<String, Object>) rsp.getData();
        return rspMap;
    }


}
